﻿Public Class FMKelas

    'member variables
    Private _cKelas As CKelas
    Private _dView As DataView

    'forms
    Private WithEvents _ftbKelas As FTbKelas
    Private WithEvents _frKelas As FRKelas

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Me.Text = My.Settings.NamaAplikasi & " [Master Kelas]"
        _cKelas = New CKelas
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, PanelJudul.MouseDown, MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint, PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If ctl.Visible Then
                p.Color = ctl.BackColor
                If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                    g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
                ElseIf TypeOf ctl Is Panel Then
                    PaintTextBox(ctl, g, p)
                ElseIf TypeOf ctl Is GroupBox Then
                    PaintTextBox(ctl, g, p)
                End If
            End If

        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(sender As System.Object, e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(sender As System.Object, e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        Else
            btnMinimize.Visible = False
            btnMaximize.Visible = False
            btnClose.Visible = False
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub FMKelas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'load data ke datagrid
        TampilkanData()
    End Sub

    Private Sub TampilkanData()
        _dView = _cKelas.getAll.DefaultView
        Me.DGMaster.DataSource = _dView
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub TambahToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TambahToolStripMenuItem.Click
        'form cover
        Dim fcov As New FCover
        fcov.Show()

        _ftbKelas = New FTbKelas
        _ftbKelas.ShowDialog()

        Fcov.Close()
    End Sub

    Private Sub _ftbKelas_DataTersimpan() Handles _ftbKelas.DataTersimpan
        TampilkanData()
    End Sub

    Private Sub HapusToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HapusToolStripMenuItem.Click
        'hapus data
        If MessageBox.Show("Hapus data ini?", "Peringatan", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            _cKelas.hapus(DGMaster.CurrentRow.Cells("CID").Value)
            TampilkanData()
        End If
    End Sub

    Private Sub RubahToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RubahToolStripMenuItem.Click
        'form cover
        Dim fcov As New FCover
        fcov.Show()

        _frKelas = New FRKelas(Me.DGMaster.CurrentRow.Cells("CID").Value, Me.DGMaster.CurrentRow.Cells("CNama").Value, Me.DGMaster.CurrentRow.Cells("CTingkat").Value, Me.DGMaster.CurrentRow.Cells("CKapasitas").Value)
        _frKelas.ShowDialog()

        fcov.Close()
    End Sub

    Private Sub _frKelas_DataTerupdate() Handles _frKelas.DataTerupdate
        'refresh data setelah terupdate
        Dim _indexLama As Integer = DGMaster.CurrentRow.Index
        TampilkanData()
        DGMaster.Rows(_indexLama).Selected = True
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TambahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        RubahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        HapusToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub

    Private Sub cbFilter_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbFilter.SelectedIndexChanged
        cbFilterSd.SelectedIndex = cbFilter.SelectedIndex
    End Sub

    Private Sub tbFilter_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbFilter.TextChanged
        'filter
        If cbFilter.SelectedIndex > 0 Then
            _dView.RowFilter = cbFilterSd.Text & " = " & tbFilter.Text.Trim
        Else
            _dView.RowFilter = cbFilterSd.Text & " like '%" & tbFilter.Text.Trim & "'"
        End If

    End Sub
End Class